<%+header%>
<style>
    @media screen and (min-width:1024px) and (max-width:1366px){
        html{
            height:150%;
        }
    }
    @media screen and (min-width:1366px) and (max-width:1440px){
        html{
            height:145%;
        }
    }
    @media screen and (min-width:1440px) and (max-width:1600px){
        html{
            height:140%;
        }
    }
    @media screen and (min-width:1600px) and (max-width:1920px){
        html{
            height:130%;
        }
    }
    @media screen and (min-width:1920px) and (max-width:2000px){
        html{
            height:100%;
        }
    }
    .DHCPstatic{
        float: left;
        width: 166px;
        margin: 5px 34px;
    }
</style>
<div class="DHCPContainers">
    <div class="allot">
        <div class="title">
            <span>已分配的DHCP租约</span>
        </div>
        <div class="titLine" style="width: 100%;height: 2px;"></div>
        <table class="hasAllotLists">
            <tr>
                <th>主机名</th>
                <th>IPv4地址</th>
                <th>MAC地址</th>
                <th>连接状态</th>
                <th>剩余租期</th>
            </tr>
            <tr class="F5ing">
                <td style="color:#b1b1b1;">正在刷新...</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </div>

    <div class="DHCPAddr">
        <div class="title">
            <span>静态地址分配</span>
        </div>
        <div class="titLine" style="width: 100%;height: 2px;"></div>

        <table class="DHCPAddrLists">
            <tr>
                <th>主机名</th>
                <th>IPv4地址</th>
                <th>MAC地址</th>
                <th>操作</th>
            </tr>
        </table>

        <form action="" id="addStaticItem">
            <div class="DHCPsta">
                <input type="text" class="DHCPstatic DHCPInput DHCPstaHost" name="cbid.dhcp.Host.name">
                <input type="text" class="DHCPstatic DHCPInput DHCPstaIp4" name="cbid.dhcp.Host.ip">
                <input type="text" class="DHCPstatic DHCPInput DHCPstaMac" name="cbid.dhcp.Host.mac">
            </div>
            <div>
                <button type="submit" class="DHCPAddAddr">新建</button>
            </div>
        </form>
    </div>
    <form method="post" action="" enctype="multipart/form-data">
        <div class="dynamicAddr">
            <div class="title">
                <span>动态地址分配</span>
            </div>
            <div class="titLine" style="width: 100%;height: 2px;"></div>

            <div class="dynamicBlock">
                <span>起始IP</span>
                <input type="number" placeholder="" class="DHCPInput DHCPStart" name="cbid.dhcp.lan.start">
            </div>
            <div class="dynamicBlock">
                <span>分配IP数量</span>
                <input type="number" placeholder="" class="DHCPInput DHCPIPNum" name="cbid.dhcp.lan.limit">
            </div>
            <div class="dynamicBlock">
                <span>DHCP租期</span>
                <input type="text" placeholder="" class="DHCPInput DHCPLease" name="cbid.dhcp.lan.leasetime">
            </div>
            <div class="DHCPOp">
                <button type="submit" class="DHCPSaveApply">保存并应用</button>
                    <button type="submit" class="DHCPSaveReset">取消</button>
            </div>
        </div>
</div>
</form>
<script>
    var itemss=sessionStorage.getItem("index");
    $(window).scroll(function(){
        var topp = $(document).scrollTop();
        $('.DHCPContainers').css('top',490-topp);
        $('#type'+ itemss).siblings().css('top',174-topp);
        if(topp > 65){
            $('header').css('visibility',"hidden");
        }else{
            $('header').css('visibility',"visible");
        }
    })
</script>
<script>
    var h=<%=host%>
    var l=<%=lan%>
    console.log(h)
    console.log(l)
    var iwxhr = new XHR();
    var wifidevs = <%=luci.http.write_json(netdevs)%>;
    var arptable = <%=luci.http.write_json(arpcache)%>;
    XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "status", "overview")%>', { status: 1 },
        function(x, info){
            var hasAllotLists = $('.hasAllotLists');
            var hasAlloTables = info.leases;
            console.log(hasAlloTables);
            $('.hasAllotLists').children().children('.hasALists').remove();
            if((hasAlloTables.length < 1)||(JSON.stringify(hasAlloTables) == "{}")){
                $('.F5ing').remove();
                var htmls = '<tr class="hasALists"><td style="color:#b1b1b1;">暂无分配的DHCP</td><td></td><td></td><td></td><td></td></tr>';
                hasAllotLists.append(htmls);
            }else{
                $('.F5ing').remove();
                for(var t = 0;t < hasAlloTables.length; t++){
                    if(hasAlloTables[t].status == 1){
                        html = '<tr class="hasALists">' +
                            '                <td>' + hasAlloTables[t].hostname + '</td>' +
                            '                <td>' + hasAlloTables[t].ipaddr + '</td>' +
                            '                <td>' + hasAlloTables[t].macaddr + '</td>'
                        html += '<td>已连接</td><td>'
                        var expiresTime = hasAlloTables[t].expires;
                        var m = secondToDate(expiresTime);
                        html += '' + m + '</td></tr>'

                    }else{
                        html = '<tr class="hasALists">' +
                            '                <td  style="color:#b1b1b1;">' + hasAlloTables[t].hostname + '</td>' +
                            '                <td  style="color:#b1b1b1;">' + hasAlloTables[t].ipaddr + '</td>' +
                            '                <td  style="color:#b1b1b1;">' + hasAlloTables[t].macaddr + '</td>'
                        html += '<td style="color:#b1b1b1;">未连接</td><td style="color:#b1b1b1;">'
                        var expiresTime = hasAlloTables[t].expires;
                        var m = secondToDate(expiresTime);
                        html += '' + m + '</td></tr>'
                    }

                    hasAllotLists.append(html);
                }
            }
        });

    $('.DHCPAddAddr').on('click',function () {
        var hostStr = $('.DHCPstaHost').val();
        var IPStr = $('.DHCPstaIp4').val();
        var MACStr = $('.DHCPstaMac').val();

        var hostName = /^[-0-9a-zA-Z]*$/g;
        var IPv4Name = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
        var MACName = /^([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}$/;

        if((hostStr=='')||(IPStr=='')||(MACStr=='')){
            layer.open({
                title: '提示信息'
                ,content: '输入内容不能为空,请重新输入'
            });
            return false;
        }else{
            if(!hostName.test(hostStr)){
                layer.open({
                    title: '提示信息'
                    ,content: '输入主机名称格式不正确,请重新输入'
                });
                return false;
            }else if(!IPv4Name.test(IPStr)){
                layer.open({
                    title: '提示信息'
                    ,content: '输入IP地址格式不正确,请重新输入'
                });
                return false;
            }else if(!MACName.test(MACStr)){
                layer.open({
                    title: '提示信息'
                    ,content: '输入MAC地址格式不正确,请重新输入'
                });
                return false;
            }else{
                layer.open({
                    title: '提示信息'
                    ,content: '添加成功'
                });
                registPost();
                event.preventDefault();
            }
        }
    });




    function registPost () {
        $.ajax({
            type: "post",
            url: "<%=REQUEST_URI%>",
            data: $('#addStaticItem').serialize()
        }).success(function(message) {
            console.log(message);
            var addDHCPstatic = Total();
            var DHCPAddrLists = $('.DHCPAddrLists');
            var staticHtml = '<tr class="DHCPstatics">' +
                '                <td name="">' + addDHCPstatic[0] + '</td>' +
                '                <td name="">' + addDHCPstatic[1] + '</td>' +
                '                <td name="">' + addDHCPstatic[2] + '</td>' +
                '                <td><button class="deleteBtn" name="cbid.dhcp.Host.del">删除</button></td></tr>'
            DHCPAddrLists.append(staticHtml);
            var DHCPStaticVal = $('.DHCPstatic');
            for (var j = 0; j < DHCPStaticVal.length; j++) {
                DHCPStaticVal.eq(j).val('');
            }
            window.location.reload();
        }).fail(function(err){
            console.log(err);
        })
    }
    function showStatic(data){
        var DHCPStaticLists =  $('.DHCPAddrLists');
        for(var d = 0;d < data.length; d++){
            var StaticModel = '<tr class="DHCPstatics">' +
                '                <td name="cbid.dhcp.Host.name' + d +'" class="dataName">' + data[d].name + '</td>' +
                '                <td name="cbid.dhcp.Host.ip' + d +'" class="dataIp">' + data[d].ip + '</td>' +
                '                <td name="cbid.dhcp.Host.mac' + d +'" class="dataMac">' + data[d].mac + '</td>' +
                '                <td><button class="deleteBtn" name="cbid.dhcp.Host.del" del="'+ data[d].del +'">删除</button></td></tr>'
            DHCPStaticLists .append(StaticModel);
        }

        $('.deleteBtn').click(function () {
            var dataName = $(this).parent().siblings('.dataName').attr('name');
            var $this = $(this);
            var dataDel = $(this).attr('del');
            console.log(dataDel);
            $.ajax({
                type: "post",
                url: "<%=REQUEST_URI%>",
                data: "cbid.dhcp.Host.del=" + dataDel
            }).success(function(message) {
                console.log(message);
                $this.parent().parent().remove();
				layer.open({
                    title: '提示信息'
                    ,content: '删除成功'
                });
            }).fail(function(err){
                console.log(err);
            });
        });
    }
    showStatic(h);

    function showDynamic(data){
        $('.DHCPStart').val(data.start);
        $('.DHCPIPNum').val(data.limit);
        $('.DHCPLease').val(data.leasetime);
    }
    showDynamic(l);


    function Total() {
        var numArr = [];
        var txt = $('.DHCPstatic');
        for (var i = 0; i < txt.length; i++) {
            numArr.push(txt.eq(i).val());
        }
        console.info(numArr);
        return numArr;
    }
    function secondToDate(result) {
        var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
        var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
        var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
        return result = h + ":" + m + ":" + s;
    }
    $('.DHCPSaveApply').on('click',function(){
        var test = $('.DHCPLease').val();
        var reg1 = /^[0-9h]+$/;
        if(!reg1.test(test)){
            layer.open({
                title: '提示信息'
                ,content: '请输入正确的时间长度'
            });
            return false;
        }else{
            if((test.substr(test.length-1,1) == 'h')||(test.substr(test.length-1,1) == 'm')){
                var loading = layer.load(0, {shade: false});
                return ture;
            }else {
                layer.open({
                    title: '提示信息'
                    ,content: '请输入正确的时间长度'
                });
                return false;
            }
        }
    });
</script>
<%+footer%>